package zhoug.zui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import zhoug.logging.Logger;
import zhoug.zui.alpha.ZUIAlphaLineaLayout;
import zhoug.zui.utils.ZUIUtils;

import androidx.annotation.DrawableRes;

/**
 * 展开按钮:更多>
 *
 * @Author 35574
 * @Date 2020/8/19
 * @Description
 */
public class ZUIExpandButton extends ZUIAlphaLineaLayout implements View.OnClickListener {
    private static final String TAG = ">>>ZUIExpandButton";
    private static  boolean debug = false;
    private @DrawableRes
    int icon;//非展开图标
    private @DrawableRes
    int expandIcon;//展开图标
    private boolean expand;//展开状态
    private String text;//非展开文本
    private String expandText = null;//展开文本
    private int space;//文字和图标的距离
    private float textSize;//文本大小
    private int textColor;//文本颜色
    private int iconSize;//图标大小

    private TextView textView;
    private ImageView imageView;

    private OnExpandChangeListener onExpandChangeListener;

    public static void setDebug(boolean debug) {
        ZUIExpandButton.debug = debug;
    }

    public ZUIExpandButton(Context context) {
        this(context, null);
    }

    public ZUIExpandButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.ZUIExpandButton_theme);
    }

    public ZUIExpandButton(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.ZUIExpandButton_def);
    }

    public ZUIExpandButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        parseAttr(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void parseAttr(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUIExpandButton, defStyleAttr, defStyleRes);
        icon = a.getResourceId(R.styleable.ZUIExpandButton_zui_iconDrawable, R.drawable.zui_icon_expand_n);
        expandIcon = a.getResourceId(R.styleable.ZUIExpandButton_zui_expandIconDrawable, R.drawable.zui_icon_expand_y);
        text = a.getString(R.styleable.ZUIExpandButton_android_text);
        if (text == null) {
            text = "更多";
        }
        expandText = a.getString(R.styleable.ZUIExpandButton_zui_expandText);
        if (expandText == null) {
            expandText = text;
        }
        space = a.getDimensionPixelSize(R.styleable.ZUIExpandButton_zui_space, 10);
        textSize = a.getDimension(R.styleable.ZUIExpandButton_zui_textSize, -1);
        textColor = a.getColor(R.styleable.ZUIExpandButton_zui_textColor, ZUIUtils.UN_USED_COLOR);
        iconSize = a.getDimensionPixelSize(R.styleable.ZUIExpandButton_zui_iconSize, 30);
        expand = a.getBoolean(R.styleable.ZUIExpandButton_zui_expand, false);
        a.recycle();

        if (debug) {
            Logger.d(TAG, "parseAttr:icon=" + icon);
            Logger.d(TAG, "parseAttr:expandIcon=" + expandIcon);
            Logger.d(TAG, "parseAttr:text=" + text);
            Logger.d(TAG, "parseAttr:expandText=" + expandText);
            Logger.d(TAG, "parseAttr:space=" + space);
            Logger.d(TAG, "parseAttr:textSize=" + textSize);
            Logger.d(TAG, "parseAttr:textColor=" + textColor);
            Logger.d(TAG, "parseAttr:iconSize=" + iconSize);
            Logger.d(TAG, "parseAttr:expand=" + expand);
        }
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        setFocusable(true);
        setClickable(true);
        setOnClickListener(this);
        //文本
        textView = new TextView(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.rightMargin = space;
        textView.setLayoutParams(params);
        ZUIUtils.setTextSize(textView, textSize);
        ZUIUtils.setTextColor(textView, textColor);
        addView(textView);
        //图标
        imageView = new ImageView(getContext());
        params = new LayoutParams(iconSize, iconSize);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        addView(imageView);

        updateExpand();

    }

    private void updateExpand() {
        if (!expand) {
            textView.setText(text);
            imageView.setImageResource(icon);
        } else {
            textView.setText(expandText);
            imageView.setImageResource(expandIcon);
        }
    }

    @Override
    public void onClick(View v) {
        expand = !expand;
        updateExpand();
        if(onExpandChangeListener!=null){
            onExpandChangeListener.onExpand(v,expand);
        }
    }

    /**
     * 展开状态
     * @return
     */
    public boolean isExpand() {
        return expand;
    }

    public void setExpand(boolean expand) {
        this.expand = expand;
        updateExpand();
    }

    public void setText(String text,String expandText){
        this.text = text;
        this.expandText = expandText;
        updateExpand();
    }



    public void setOnExpandChangeListener(OnExpandChangeListener onExpandChangeListener) {
        this.onExpandChangeListener = onExpandChangeListener;
    }


    /**
     * 展开状态监听器
     */
    public interface OnExpandChangeListener {
        void onExpand(View view,boolean expand);
    }

}
